use serde::{Deserialize, Serialize};

/// 系统配置
#[derive(Debug, Deserialize)]
pub struct Configs {
    /// 程序配置
    pub server: Server,
    ///  数据库 配置
    pub database: Database,
    pub redis: RedisConfig,
    ///  JWT 配置
    pub jwt: Jwt,
    /// 日志配置
    pub log: Log,
    /// RPC 配置 - 现在使用 utils::rpc::RpcConfig
    /// 注意：已迁移到 utils crate，此字段保留用于配置文件兼容
    pub rpc: serde_json::Value,
}

/// 程序配置
#[derive(Debug, Deserialize)]
pub struct Server {
    /// 环境 dev 或 release
    pub env: String,
    /// 服务器名称
    pub name: String,
    /// 服务器(IP地址:端口)
    /// `0.0.0.0:3000`
    pub address: String,
    pub port: String,
    /// 服务器ssl
    pub ssl: bool,
    /// 响应数据gzip
    pub content_gzip: bool,
    /// 缓存时间
    pub cache_time: u64,
    /// 缓存方式
    pub cache_method: u32,
    /// api 前缀  例如："/api_v1"
    pub api_prefix: String,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RedisPool {
    #[serde(default = "RedisPool::max_size_default")]
    pub max_size: usize,
    #[serde(default = "RedisPool::mini_idle_default")]
    pub mini_idle: usize,
    #[serde(default = "RedisPool::connection_timeout_default")]
    pub connection_timeout: usize,
}

impl Default for RedisPool {
    fn default() -> Self {
        Self {
            max_size: 1,
            mini_idle: 1,
            connection_timeout: 10,
        }
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RedisConfig {
    pub server: String,
    pub password: String,
    pub db: i64,
    pub port: i64,
    pub pool: Option<RedisPool>,
}

impl RedisPool {
    pub fn max_size_default() -> usize {
        1
    }

    pub fn mini_idle_default() -> usize {
        1
    }

    pub fn connection_timeout_default() -> usize {
        10
    }
}

impl RedisConfig {
    pub fn default() -> Self {
        RedisConfig {
            server: String::from(""),
            password: String::from(""),
            db: 0,
            port: 6379,
            pool: Default::default(),
        }
    }
}

/// jwt 配置文件
#[derive(Debug, Deserialize)]
pub struct Jwt {
    /// JWT 密钥
    pub access_secret: String,
    /// JWT 过期时间
    pub access_exp: i64,
    /// JWT 密钥
    pub refresh_secret: String,
    /// JWT 过期时间
    pub refresh_exp: i64,
}

/// 日志配置
#[derive(Debug, Deserialize)]
pub struct Log {
    /// `log_level` 日志输出等级
    pub log_level: String,
    /// 允许操作日志输出
    pub enable_oper_log: bool,
    // 是否高亮
    pub with_ansi: bool,
    // 是否显示控制台，false输出文件
    pub to_stdout: bool,
    pub directory: String,
    pub file_name: String,
    pub rolling: String,
}

/// 数据库
#[derive(Debug, Deserialize)]
pub struct Database {
    /// 数据库连接
    pub link: String,
}
